<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\service\v1\OptionService;
use plugin\admin\app\service\v1\UserWithdrawalService;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1UserWithdrawal;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 提现记录
 */
class V1UserWithdrawalController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1UserWithdrawal
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1UserWithdrawal;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        $admin = admin();
        return view('v1-user-withdrawal/index',compact('admin'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('v1-user-withdrawal/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-user-withdrawal/update');
    }

    /**
     * 申请提现
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function create(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $amount = $request->post('amount');
            $account_id = (integer)$request->post('account_id', 0);
            $desc = $request->post('desc', '');
            if (!is_numeric($amount) || $amount < 0) {
                return $this->json(1, '请输入提现金额！');
            }
            if (!$account_id) {
                return $this->json(1, '请选择支付宝账号！');
            }
            $config = OptionService::getConfig('withdrawal');
            $withdrawal_money = $config['withdrawal_money'] ?? 0;
            if ($withdrawal_money > 0 && $withdrawal_money > $amount) {
                return $this->json(1, "提现金额不能小于{$withdrawal_money}元！");
            }
            $params = compact('amount', 'account_id', 'desc');

            $lock = Util::basicSetnx('withdrawal_user_'.admin_id(),1);
            if (!$lock) {
                return json(['code' => 1, 'msg' => '系统繁忙，请稍后再试！']);
            }

            $admin = admin();
            if ($admin['user_status'] == CommonConstant::DEFAULT_1) {
                return $this->json(1, '用户不是会员！');
            }
            UserWithdrawalService::create($params, $admin);
            return $this->json(0, '提现申请成功，请耐心等待审核');
        }
        $admin = admin();
        return view('v1-user-withdrawal/create', compact('admin'));
    }

    /**
     * 提现审核
     * @param Request $request
     * @return Response
     * @throws BusinessException
     **/
    public function audit(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $id = (integer)$request->post('id',0);
            $status = (integer)$request->post('status', 0);
            $audit_desc = $request->post('audit_desc', '');
            if (!$id) {
                return $this->json(1, '提现参数错误！');
            }
            if (!in_array($status,[CommonConstant::DEFAULT_2,CommonConstant::DEFAULT_3])) {
                return $this->json(1, '审核状态错误！');
            }
            if (!$audit_desc) {
                return $this->json(1, '请输入审核备注！');
            }
            $params = compact('id','status','audit_desc');
            $admin = admin();
            if ($admin['user_status'] != CommonConstant::DEFAULT_1) {
                return $this->json(1, '用户不是管理员！');
            }
            UserWithdrawalService::audit($params,$admin);
            return $this->json(0, $status == CommonConstant::DEFAULT_2 ? '提现成功':'提现驳回');
        }
    }

}
